//
//  DUAddPopPanel.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/24.
//

import Cocoa

class DUAddPopPanel: NSWindowController,NSWindowDelegate {

    
    @IBOutlet weak var fullName: NSTextField!
    @IBOutlet weak var sortName: NSTextField!
    @IBOutlet weak var lunarOrSolar: NSPopUpButton!
    @IBOutlet weak var monthBox: NSPopUpButton!
    @IBOutlet weak var daybox: NSPopUpButton!
    @IBOutlet weak var holiday: NSSwitch!
    @IBOutlet weak var remark: NSTextField!
    
    private let solarMonthStr = ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"]
    private let lunarMonthStr = ["正月","二月","三月","四月","五月","六月","七月","八月","九月","十月","冬月","腊月"]
    
    
    override var windowNibName: NSNib.Name? {
      return "DUAddPopPanel"
    }
    
    override func windowDidLoad() {
        super.windowDidLoad()
        self.window?.center()
        self.window?.makeKeyAndOrderFront(nil)
        NSApplication.shared.activate(ignoringOtherApps: true)
        self.window?.isRestorable = false
        lunarOrSolar.selectItem(at: 0)
        monthBox.removeAllItems()
        daybox.removeAllItems()
        monthBox.addItems(withTitles: solarMonthStr)
        monthBox.selectItem(at: 0)
        daybox.addItems(withTitles: getMonthDays(month: 1))
    }
    
    
    func windowWillClose(_ notification: Notification) {
        GlobalControl.shared.system?.enableAll(e: false)
        NSApplication.shared.stopModal()
    }
    
    @IBAction func change(_ sender: NSPopUpButton) {
        if sender.identifier?.rawValue == "lunarOrSolar" {
            let item = sender.selectedItem
            monthBox.removeAllItems()
            daybox.removeAllItems()
            if item?.identifier?.rawValue == "solar" {
                monthBox.addItems(withTitles: solarMonthStr)
                monthBox.selectItem(at: 0)
                daybox.addItems(withTitles: getMonthDays(month: 1))
            } else if item?.identifier?.rawValue == "lunar" {
                monthBox.addItems(withTitles: lunarMonthStr)
                monthBox.selectItem(at: 0)
                daybox.addItems(withTitles: CalendarUtil.LUNAR_DAY_TO_STRING)
            }
        } else if sender.identifier?.rawValue == "month" {
            let i = sender.indexOfSelectedItem
            if i == -1 {
                return
            }
            if lunarOrSolar.selectedItem?.identifier?.rawValue == "lunar" {
                return
            }
            daybox.removeAllItems()
            daybox.addItems(withTitles: getMonthDays(month: i + 1))
            
        }
    }
    
    @IBAction func holidayClick(_ sender: NSSwitch) {
        
    }
    
    
    private func getMonthDays(month: Int) -> [String] {
        var s = [String]()
        let o = CalendarUtil.getMonthDays(year: 2021, month: month)
        for i in 1...o {
            s.append("\(i)日")
        }
        return s
    }
    
}
